import unittest
from sirogi.letter.models import Letter
from sirogi.users.models import SirogiUser
from django.test.client import Client
from django.test import TestCase


class BasicTestCase(unittest.TestCase):
  
    def test_write_a_letter_unauthorized_user(self):
        c = Client()
        
        #llamo a la pagina
        response = c.get('/letter/')
        #302 me redirecciona a la pagina de login.
        self.failUnlessEqual(response.status_code, 302)


    def test_write_a_letter_authorized_user(self):
        c = Client()
        
        #logueo un usuario valido
        self.failUnlessEqual(c.login(username='inspector', password='inspector'), True)
        
        #llamo a la pagina
        response = c.get('/letter/')
        
        #200 http response
        self.failUnlessEqual(response.status_code, 200)

        #verifico que la pagina retornada sea la de escribir letras
        self.failUnlessEqual("Agregar un nuevo documento" in response.content, True)

        #lleno los campos de la carta en los envio
        response = c.post('/letter/save/',{'recipient':'manager', 'subject': 'subject', 'body': 'body'})

        #verifico que se envio correctamente la carta
        self.failUnlessEqual("Sent Letter" in response.content, True)
        
    def test_read_a_letter_unauthorized_user(self):
        c = Client()
        
        #llamo a la pagina
        response = c.get('/letter/read/4/')
        #302 me redirecciona a la pagina de login.
        self.failUnlessEqual(response.status_code, 302)
    
    def test_read_a_letter_unauthorized_user_log(self):
        c = Client()
        #pruebo con un usuario que no sea el due;o de las cartas pero que este logueado

        self.failUnlessEqual(c.login(username='editor', password='editor'), True)
        #llamo a la pagina
        response = c.get('/letter/read/4/')
        #302 me redirecciona a la pagina de login.
        self.failUnlessEqual('You dont have permission for this action' in response.content, True)
 
    def test_read_unexisted_letter(self):
        c = Client()
        self.failUnlessEqual(c.login(username='editor', password='editor'), True)
        #llamo a la pagina
        response = c.get('/letter/read/1872726254/')
        self.failUnlessEqual('You do not have permission for this action' in response.content, True)
        

